﻿Imports Entities.OpenMediaInfo

Public Class FTest2
    Private mMediaInfo As Entities.OpenMediaInfo.OpenMediaInfo

    Private Sub FTest2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.WindowState = FormWindowState.Maximized
    End Sub

    Private Sub btnGetInfo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGetInfo.Click
        WebBrowser1.Navigate(ComboBox1.Text)
        GetMovieInfo()
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        GetMovieInfo()
    End Sub

    Private Sub GetMovieInfo()
        Dim clsMI As Entities.OpenMediaInfo.OpenMediaInfo

        If ComboBox1.Text.Trim.ToLower.StartsWith("http") Then
            Dim clsUri As New Uri(ComboBox1.Text)
            clsMI = OpenMediaInfo.Manager.GenerateForMovie(clsUri)
        Else
            Dim clsUri As New Uri("file://" & ComboBox1.Text)
            clsMI = OpenMediaInfo.Manager.GenerateForMovie(clsUri)
        End If

        With clsMI.Feature
            txtIds.Text = Utils.Serialization.ToCommaSepString(.Ids)

            FillTextBox(txtDirectors, .Directors)
            FillTextBox(txtWriters, .Writers)
            FillTextBox(txtCast, .Cast)

            FillTextBox(txtGenre, .Genres)

            FillTextBox(txtCertification, .Certifications)

            txtTagline.Text = .Tagline
            txtPlot.Text = .PlotSummary
            txtReleaseDate.Text = .ReleaseDate
            txtDescription.Text = .Description
            txtTitle.Text = .Title
            txtYear.Text = .Year

            If .Poster Is Nothing Then
                picPoster.Image = Nothing
            Else
                Using clsStream As New IO.MemoryStream(.Poster.Data)
                    Dim clsImage As New Bitmap(clsStream)
                    picPoster.Image = clsImage
                End Using
            End If
        End With
        mMediaInfo = clsMI
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        WebBrowser1.Navigate(ComboBox1.Text)
    End Sub


    Private Sub FillTextBox(ByVal textBox As TextBox, ByVal list As Generic.List(Of Contributer))
        textBox.Text = String.Empty
        For Each clsPerson As Contributer In list
            If textBox.Text.Length > 0 Then textBox.Text = textBox.Text & vbNewLine
            Dim strType As String = clsPerson.Part
            If Not String.IsNullOrEmpty(strType) Then strType = String.Format(" ({0})", strType)
            textBox.Text = textBox.Text & String.Format("{0}{2} [{1}]", clsPerson.Name, Utils.Serialization.ToCommaSepString(clsPerson.Ids), strType)
        Next
        textBox.Tag = list
    End Sub

    Private Sub FillTextBox(ByVal textBox As TextBox, ByVal list As Generic.List(Of Genre))
        textBox.Text = String.Empty
        For Each clsGenre As Genre In list
            If textBox.Text.Length > 0 Then textBox.Text = textBox.Text & ", "
            textBox.Text = textBox.Text & clsGenre.Name
        Next
        textBox.Tag = list
    End Sub

    Private Sub FillTextBox(ByVal textBox As TextBox, ByVal list As Generic.List(Of Certification))
        textBox.Text = String.Empty
        For Each clsCert As Certification In list
            If textBox.Text.Length > 0 Then textBox.Text = textBox.Text & ", "
            Dim strNote As String = Nothing
            If Not String.IsNullOrEmpty(clsCert.Note) Then strNote = String.Format(" ({0})", clsCert.Note)
            textBox.Text = String.Format("{0}{1}:{2}{3}", textBox.Text, clsCert.Country, clsCert.Level, strNote)
        Next
        textBox.Tag = list
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        If mMediaInfo Is Nothing Then Beep() : Return
        Dim strFilename As String
        SaveFileDialog1.Filter = "Open Media Xml|*.omx"
        SaveFileDialog1.Title = "Save Open Media Info"
        SaveFileDialog1.FileName = String.Format("{0} ({1}).omx", mMediaInfo.Feature.Title, mMediaInfo.Feature.Year)
        SaveFileDialog1.CheckFileExists = False
        Select Case SaveFileDialog1.ShowDialog(Me)
            Case Windows.Forms.DialogResult.OK
                strFilename = SaveFileDialog1.FileName
            Case Else
                Return
        End Select

        Dim strXml As String = Utils.Serialization.ToXml(mMediaInfo)
        Using fs As New IO.FileStream(SaveFileDialog1.FileName, IO.FileMode.Create)
            Using tw As New IO.StreamWriter(fs, System.Text.Encoding.Unicode)
                tw.Write(strXml)
            End Using
        End Using
    End Sub
End Class